package com.hospital.vac.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.vac.common.Result;
import com.hospital.vac.entity.Notification;
import com.hospital.vac.entity.Permission;
import com.hospital.vac.mapper.NotificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description userId{1为系统消息,2为医院通知，其他为用户订阅}
 * @return
 * @author ANLGIN
 * @since 2023/3/12 14:24
 */

@RestController
@RequestMapping("/notify")
public class NotificationController {
    @Resource
    private NotificationMapper notificationMapper;

    @GetMapping
    public Result<?> getNotificationsByPage(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "search", defaultValue = "") String search) {
        LambdaQueryWrapper<Notification> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Notification::getMessage, search);
        }
        Page<Notification> notificationPage = notificationMapper.selectPage(new Page<Notification>(page, size), wrapper);
        return Result.success(notificationPage);
    }

    @PostMapping
    public Result<?> createNotification(@RequestBody Notification notification) {
        notificationMapper.insertNotification(notification);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Notification notification) {

        notificationMapper.updateById(notification);
        return Result.success();
    }

    /**
     * @description 订阅消息
     * @return com.hospital.vac.common.Result<java.util.List < com.hospital.vac.entity.Notification>>
     * @author ANLGIN
     * @since 2023/3/12 14:23
     */

    @GetMapping("/{userId}")
    public Result<List<Notification>> getNotificationsByUserId(@PathVariable Long userId) {
        List<Notification> notifications = notificationMapper.getNotificationsByUserId(userId);
        return Result.success(notifications);
    }
    /**
     * @description 系统消息
     * @return com.hospital.vac.common.Result<?>
     * @author ANLGIN
     * @since 2023/3/12 14:21
     */
    @GetMapping("/banner")
    public Result<?> getAllNotification(){
        LambdaQueryWrapper<Notification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notification::getUserId,1);
        List<Notification> notifications = notificationMapper.selectList(wrapper);
        return Result.success(notifications);
    }
    /**
     * @description 医院通知
     * @return com.hospital.vac.common.Result<?>
     * @author ANLGIN
     * @since 2023/3/12 14:23
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        LambdaQueryWrapper<Notification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notification::getId, id);
        notificationMapper.delete(wrapper);
        return Result.success();
    }
}